<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 控件数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\OriginPlanNodeService;
use common\service\WorkTimeRecordService;
use think\Request;

class OriginalPlanNodeController extends VerifyController
{
    /**
     * @var WorkTimeRecordService
     */
    private $originPlanNodeService;

    public function __construct()
    {
        parent::__construct();
        $this->originPlanNodeService = OriginPlanNodeService::getInstance();
    }

    /**
     * 补充原计划数据
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function bathUpdateOriginPlanNodeInfo(Request $request)
    {
        $param = $request->param();
        $async = $param['async'] ?? true;
        $res = $this->originPlanNodeService->bathUpdateOriginPlanNodeInfo($param, $async);
        return json(success_response('', $res));
    }

    /**
     * 原计划排期节点---日报
     */
    public function getDailyList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $request->param()['param']['page'] ?? [1, C('default_page_total')];

        try {
            $resData = $this->originPlanNodeService->getDailyList($param, $page);
            return json(success_response('OK', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 原计划排期节点---日报--统计
     */
    public function getDailyStatList(Request $request)
    {
        $param = $request->param()['param'];
        $targetStepCategoryId = $param['target_step_category_id'] ?? 0;
        //环节ID
        if (!empty($param['step_category_id'])) {
            $stepCategoryIds = explode(',', $param['step_category_id']);
        } else {
            $stepCategoryIds = [0];
        }
        try {
            $resData = $this->originPlanNodeService->getDailyStat($param['project_id'], $param['start_time'], $param['end_time'], $stepCategoryIds, $targetStepCategoryId);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 原计划排期节点---日报--延期列表
     */
    public function getDailyDelayList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $request->param()['param']['page'] ?? [1, C('default_page_total')];
        try {
            $resData = $this->originPlanNodeService->getDailyDelayList($param, $page);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 通过实体获取任务
     */
    public function getEntityTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $request->param()['param']['page'] ?? [1, C('default_page_total')];
        try {
            $resData = $this->originPlanNodeService->getEntityTaskList($param, $page);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 日报--通过任务ID查询延期记录
     */
    public function getDelayListByTaskId(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getDelayListByTaskId($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 日报--通过任务ID查询工单记录
     */
    public function getWorkOrderListByTaskId(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getWorkOrderListByTaskId($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 根据延期记录id，获取延期确认列表
     */
    public function getDelayOrderTaskStepCategoryConfirm(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getDelayOrderTaskStepCategoryConfirm($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 根据工单id，获取问题环节确认列表
     */
    public function getTaskWorkOrderProblemStepCategory(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getTaskWorkOrderProblemStepCategory($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取需求单列表 --- 是否标准新增
     */
    public function getDemandOrderDelayList(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getDemandOrderDelayList($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取标准主任务信息---通过子任务
     */
    public function getSourceTaskList(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getSourceTaskList($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 日报上报表--上报（备注）
     */
    public function planNodeReport(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->planNodeReport($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 日报上报表--上报列表
     */
    public function planNodeReportList(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->planNodeReportList($param);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 获取异常任务列表
     */
    public function getAbnormalTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $param['page'] ?? [1, C('default_page_total')];
        $moduleCode = $param['module_code'] ?? '';
        try {
            $resData = $this->originPlanNodeService->getAbnormalTaskList($param['project_id'], $param['time_range'], $moduleCode, $page);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取异常任务--未处理的工单列表数据
     */
    public function getAbnormalTaskWorkOrderList(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $resData = $this->originPlanNodeService->getAbnormalTaskWorkOrderList($param['project_id'], $param['time_range'], $param['task_ids']);
            return json(success_response("OK", $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 按分类统计异常任务数量最多统计按实体去重
     * @param Request $request
     * @return \think\Response
     */
    public function countAbnormalEntity(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->originPlanNodeService->countAbnormalEntity($param['entity_module_code'], $param['project_id'], $param['time_range']);
        return json(success_response("OK", $resData));
    }

    /**
     * 按分类统计异常任务数量最多统计按任务去重
     * @param Request $request
     * @return \think\Response
     */
    public function countAbnormalTask(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->originPlanNodeService->countAbnormalTask($param['step_type'], $param['project_id'], $param['time_range']);
        return json(success_response("OK", $resData));
    }


    /**
     * 获取今天存在异常的集数列表，附带异常实体数据统计
     * @param Request $request
     * @return \think\Response
     */
    public function getTodayExistAbnormalEpisodeList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->originPlanNodeService->getTodayExistAbnormalEpisodeList($param['entity_module_code'], $param['project_id']);
        return json(success_response("OK", $resData));
    }

    /**
     * 获取指定集数范围的异常实体列表数据
     * @param Request $request
     * @return \think\Response
     */
    public function getEpisodeAbnormalEntityList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $param['page'] ?? [1, 100];
        $resData = $this->originPlanNodeService->getEpisodeAbnormalEntityOrTaskList($param['entity_module_code'], $param['project_id'], (string)$param['initial_episode_code'], $param['time_range'], $page);
        return json(success_response("OK", $resData));
    }

    /**
     * 根据异常实体id获取下面异常任务列表
     * @param Request $request
     * @return \think\Response
     */
    public function getEpisodeAbnormalTaskMapByEntity(Request $request){
        $param = $request->param()['param'];
        $resData = $this->originPlanNodeService->getEpisodeAbnormalTaskMapByEntity($param['entity_module_code'], $param['project_id'], $param['entity_ids']);
        return json(success_response("OK", $resData));
    }

    /**
     * 获取指定集数范围的异常任务列表数据
     * @param Request $request
     * @return \think\Response
     */
    public function getEpisodeAbnormalTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $param['page'] ?? [1, 100];
        $resData = $this->originPlanNodeService->getEpisodeAbnormalEntityOrTaskList($param['step_type'], $param['project_id'], (string)$param['initial_episode_code'], $param['time_range'], $page);
        return json(success_response("OK", $resData));
    }

    /**
     * 根据异常任务id获取下面异常任务列表
     * @param Request $request
     * @return \think\Response
     */
    public function getEpisodeAbnormalTaskMapByTask(Request $request){
        $param = $request->param()['param'];
        $resData = $this->originPlanNodeService->getEpisodeAbnormalTaskMapByTask($param['step_type'], $param['project_id'], $param['task_ids']);
        return json(success_response("OK", $resData));
    }
}
